/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.autosalon.ui.customer.action;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.model.Adresa;
import com.autosalon.model.Zakaznik;
import com.autosalon.ui.customer.Customer;
import com.autosalon.ui.customer.state.CustomerStateHolder;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Kubík
 */
public class EditCustomerAction extends AbstractCustomerAction implements Button.ClickListener {

    private Zakaznik zakaznik;

    public EditCustomerAction(Customer customer) {
        super(customer);

    }

    @Override
    public void buttonClick(ClickEvent event) {
        zakaznik = (Zakaznik) DaoFactory.getinstance().getZakaznik().getById(Long.parseLong(event.getButton().getData().toString()));
        Button saveChanges = new Button("Ulož změny");
        saveChanges.addListener(new SaveUpdateCustomerAction(customer, zakaznik));
        customer.setBtnSaveEdistCustomer(saveChanges);
        customer.getTexFirstName().setValue(zakaznik.getMeno());
        customer.getTexLastName().setValue(zakaznik.getPrimeni());
        customer.getTexMobil().setValue(zakaznik.getMobil());
        customer.getTexEmail().setValue(zakaznik.getEmail());
        customer.getTexNote().setValue(zakaznik.getPoznamka());
        customer.getTexPassword().setValue(zakaznik.getPassword());
        customer.getTexControlPassword().setValue(zakaznik.getPassword());
        List<Adresa> adresy = new ArrayList<Adresa>();
        adresy = zakaznik.getAdresy();
        int count = 0;
        customer.getNewAdress().remove(0);
        customer.getNewAdress().removeAll(customer.getNewAdress());
        for (Adresa adresa : adresy) {
            count++;
            Form adress = new Form();
            adress.getLayout().addComponent(new Label("Adresa " + count));
            TextField stat = new TextField("Stát");
            stat.setValue(adresa.getStat().toString());
            adress.getLayout().addComponent(stat);

            TextField mesto = new TextField("Město");
            mesto.setValue(adresa.getMesto().toString());
            adress.getLayout().addComponent(mesto);

            TextField ulice = new TextField("Ulice");
            ulice.setValue(adresa.getUlice().toString());
            adress.getLayout().addComponent(ulice);

            TextField popisne = new TextField("Č. popisné");
            popisne.setValue(Integer.toString(adresa.getCp()));
            adress.getLayout().addComponent(popisne);

            TextField orientacni = new TextField("Č. orientační");
            orientacni.setValue(Integer.toString(adresa.getCo()));
            adress.getLayout().addComponent(orientacni);

            TextField psc = new TextField("PSC");
            psc.setValue(adresa.getPsc().toString());
            adress.getLayout().addComponent(psc);
            customer.getNewAdress().add(adress);
        }

        customer.addComponent();
        int id;
        try {
            id = customer.getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
        } catch (NullPointerException e) {
            id = 1;
        }
        CustomerStateHolder.setState(CustomerStateHolder.EDIT_CUSTOMER_STATE, id, super.customer);



    }
}
